﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuantitativeInvestment.Bean;
using QuantitativeInvestment.Tools;

namespace QuantitativeInvestment.Model
{
    class MaModel : Model
    {
        public MaModel()
        {
            this.name = "均线模型";
            Parameter p = new Parameter("持有时间", 30);
            this.paraList.Add(p.name, p);

            p = new Parameter("大于均线", "是");
            p.type = "enum";
            p.enumList.Add("是");
            p.enumList.Add("否");
            this.paraList.Add(p.name, p);

            this.factorList.Add(Factor.Factor.createFactorByName("均线"));
        }

        public override List<TradingStock> buy(List<TradingStock> stockList, string tradingDate)
        {

            List<TradingStock> selectedStocks = new List<TradingStock>();

          //  double stockAverage = selectedStocks.Average();
          //  int observePeriod = Int32.Parse(this.paraList["观察时间"].value.ToString());
          //string compareType = this.paraList["比较方法"].value.ToString();
            int currentPosition = this.container.tradingDates.IndexOf(tradingDate);
            string startDate= this.container.tradingDates[currentPosition];

            List<TradingStock> comparedStocks = new List<TradingStock>();
            foreach (TradingStock tradingStock in stockList)
            {
                Stock currentStock = this.container.stocks[tradingStock.code];

                int initialStartPosition = currentStock.tradingDateList.IndexOf(currentStock.startDate);

                int stockCurrentPosition = currentStock.tradingDateList.IndexOf(tradingDate);

                //如果之前没有数据，则跳过该个股，不作为选择
                if (stockCurrentPosition == -1)
                {
                    continue;
                }

                double[] maValues = this.container.stocks[tradingStock.code].factors["均线"+this.factorList[1].paraList["天数"].value.ToString()];
                double[] closePrice = this.container.stocks[tradingStock.code].factors["价格收盘价"];
                string isPlusMa = this.paraList["大于均线"].value.ToString();
                if (isPlusMa == "是")
                {
                    if (stockCurrentPosition > initialStartPosition && closePrice[stockCurrentPosition - initialStartPosition] > maValues[stockCurrentPosition - initialStartPosition])
                    {
                        double comparedValue = closePrice[stockCurrentPosition - initialStartPosition] - maValues[stockCurrentPosition - initialStartPosition];
                        tradingStock.buyDate = tradingDate;
                        selectedStocks.Add(tradingStock);
                    }
                }
                else
                {
                    if (stockCurrentPosition > initialStartPosition && closePrice[stockCurrentPosition - initialStartPosition]< maValues[stockCurrentPosition - initialStartPosition])
                    {
                        double comparedValue = closePrice[stockCurrentPosition - initialStartPosition] - maValues[stockCurrentPosition - initialStartPosition];
                        tradingStock.buyDate = tradingDate;
                        selectedStocks.Add(tradingStock);
                    }
                }
            }
            return selectedStocks;
        }

        public override List<TradingStock> sell(List<TradingStock> stockList, string tradingDate)
        {
            int holdPeriod = Int32.Parse(this.paraList["持有时间"].value.ToString());
            int buyPosition = this.container.tradingDates.IndexOf(tradingDate);
            if (buyPosition + holdPeriod >= this.container.tradingDates.Count)
                return new List<TradingStock>();

            string sellDate = this.container.tradingDates[buyPosition + holdPeriod];
            foreach (TradingStock tradingStock in stockList)
            {
                tradingStock.sellDate = sellDate;
            }
            return stockList;
        }
    }
}